using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;

namespace DSEngine.Sound
{
    public class Sound
    {
        public SoundManager Parent { get; set; }
        private SoundEffect soundEffect;
        private SoundEffectInstance soundInstance;
        internal Sound(SoundEffectInstance instance, SoundEffect effect)
        {
            soundEffect = effect;
            soundInstance = instance;
            this.IsLoop = false;
        }

        public delegate void OnSoundEnd(Sound sender);
        public event OnSoundEnd OnSoundEndEvent;
        public SoundState GetState()
        {
            return soundInstance.State;
        }
        public void Play()
        {
            soundInstance.Play();
        }
        public void Pause()
        {
            soundInstance.Pause();
        }
        public void Stop()
        {
            soundInstance.Stop();
           // if (IsLoop == false)
            {
                this.Parent.Remove(this);
            }
        }
        public void Resume()
        {
            soundInstance.Resume();
        }

        public bool IsLoop { get; set; }

        public void Update(GameTime gameTime)
        {
            if (IsLoop)
            {
                if (soundInstance.State == SoundState.Stopped)
                {
                    soundInstance.Play();
                }
            }
            else
            {
                if (OnSoundEndEvent != null)
                    OnSoundEndEvent(this);
            }
        }
    }
}
